import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {ProductProvider} from "@providers/interface/product";
import {Util} from "@providers/util";
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";

/**
 * Generated class for the NewProductPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-new-product',
  templateUrl: 'new-product.html',
})
export class NewProductPage {

  goods_name: any;//物料名称
  brands: any[];//品牌
  brand: any ;//已选品牌
  categorys: any[];//类型
  category: any;//已选类型
  JLDW:any;//计量单位
  BZ:any;//备注
  picList: any[] = []; //手机选择的图片
  userInfo:any;//师傅信息

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl:ViewController,
              private dialog: DialogProvider,
              private storage:StorageProvider,
              private product: ProductProvider) {
    this.userInfo= this.storage.read("userInfo");
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad NewProductPage');
    this.getBrands();
    this.getCategorys();
  }

  dismiss(data?: any) {
    if (data) {
      this.viewCtrl.dismiss(data);
      return;
    }
    this.viewCtrl.dismiss();
  }

  submit() {
    if (Util.isNull(this.goods_name)) {
      this.dialog.showAlert('请输入物品名称');
      return;
    }
    let pic: any[] = [];
    for (let i = 0; i < this.picList.length; i++) {
      pic.push(this.picList[i]["url"])
    }

    let json: any = {
      "SPMC": this.goods_name,
      "PPB02": this.brand?this.brands[this.brand].BRANDNAME:'',
      "PPB01": this.brand?this.brands[this.brand].PRODUCT_BRAND_ID:'',
      "SPFL01": this.category?this.categorys[this.category].PRODUCT_CATEGORY_ID:'',
      "SPFL02": this.category?this.categorys[this.category].CATEGORY_NAME:'',
      "JLDW": this.JLDW,
      "BZ": this.BZ,
      "SPTP": pic.join(','),
      "SQR": this.userInfo.USERNAME,
      "SQRID": this.userInfo.EXTID
    };
    this.product.seaveNewProduct(json).then((data) => {
      this.dialog.showAlert('申请已发出等待审核');
      this.dismiss();
    }).catch((error) => {})
  }


  getCategorys() {
    this.product.getCategorys({MJBJ:1}).then((data) => {
      this.categorys = JSON.parse(JSON.stringify(data.data));
    }).catch(() => {});
  }

  getBrands() {
    this.product.getBrands({}).then((data) => {
      this.brands = JSON.parse(JSON.stringify(data.data));
    }).catch(() => {});
  }

}
